package game_FrozenBubble;

import java.util.Vector;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.TextView;

import com.BioSensor.SensorConnectionManager;
import com.gmsi_prototype.R;

public class CalibrationActivity extends Activity {

	private CountDownTimer cdTimer;
	private BluetoothAdapter mBluetoothAdapter;
	private long calibrationTime;
	private SensorConnectionManager scm;
	private String[] sensor_name;
	private static final int REQUEST_ENABLE_BT = 3;
	private TextView tvTimer;
	private boolean stillContinue;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_activity_calibration);
		
		getIntent();
		tvTimer = (TextView) findViewById(R.id.tvTimer);

		calibrationTime = (long) GlobalConstant.windowShimmer * 1000;
		stillContinue = true;
		
		Thread t = new Thread(new Runnable() {
			
			public boolean stillContinue;

			@Override
			public void run() {
				init();
				runBT();
				
				while(stillContinue){}
				GlobalConstant.isCalibrated = true;
				Vector<float[]> thresholds = new Vector<float[]>();
				for(String s: scm.getConnectedSensors()) {
					float[] t = scm.getThresholds(s, scm.getDeviceSensors(s));
					thresholds.add(t);
				}
				GlobalConstant.thresholds = thresholds;
				Log.d("Threshold", "Finished Calibration");
				scm.disconnectSensors(sensor_name);
				finishActivity();
			}
		}
		);
		t.start();
		
		cdTimer = new CountDownTimer(calibrationTime, 1000) {
			
			public void onTick(long millisUntilFinished) {
				tvTimer.setText("seconds remaining: " + millisUntilFinished / 1000);
			}
			
			public void onFinish() {
				tvTimer.setText("done!");
				stillContinue = false;
			}
		}.start();
	}
	
	private void runBT() {
		try {
			Log.d("Threshold", sensor_name[0]);
			scm.connectSensors(sensor_name);
		} catch (IllegalArgumentException e) {
			Log.d("Exception",e.getMessage());
		}
		scm.calibrateAll();
	}

	public void init() {
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		
		while (!mBluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
		}
		scm = new SensorConnectionManager(mBluetoothAdapter);
		sensor_name = null;
		Vector<String> vSenorNames = new Vector<String>();
		if(GlobalConstant.isCheck_BH_BR || GlobalConstant.isCheck_BH_HR || GlobalConstant.isCheck_BH_HRV) 
			vSenorNames.add("BioHarness");
		if(GlobalConstant.isCheck_Shim_SCL || GlobalConstant.isCheck_Shim_SCR) 
			vSenorNames.add("Shimmer");
		sensor_name = vSenorNames.toArray(new String[vSenorNames.size()]);
	}
	
	public void finishActivity() {
		Intent sa = new Intent(CalibrationActivity.this, ChillOut.class);
		startActivity(sa);
		finish();
	}
}
